package pers.mingshan.thread.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @author mingshan
 *
 */
public class LocalTest {

    public static void main(String[] args) {
        new ThreadSynchronizedDemo().init();
    }

    public void init() {
       final outPuter o = new outPuter();
       new Thread(new Runnable() {
           @Override
           public void run() {
               while(true) {
                   try {
                       Thread.sleep(1000);
                       o.outPut("hanjuntao");
                    } catch (InterruptedException e) {
                       e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        o.outPut("mahuateng");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    static class outPuter {
        //锁
        Lock lock=new ReentrantLock();

        public  void outPut(String name) {
            int len=name.length();
            lock.lock();
            try {
                for (int i = 0; i < len; i++) {
                    System.out.print(name.charAt(i));
                }
                System.out.println();
            } finally {
                lock.unlock();
            }
        }
    }
}
